package com.zt.rongda.web.service;

import com.zt.rongda.constant.BusConstant;
import com.zt.rongda.maintenance.inter.MaintenanceApplyMapper;
import com.zt.rongda.maintenance.model.MaintenanceApply;
import com.zt.rongda.sign.inter.SignMapper;
import com.zt.rongda.sign.model.Sign;
import com.zt.rongda.util.JSONUtil;
import com.zt.servlet.ReqBean;
import com.zt.system.model.BusinessException;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.StringUtil;
import java.util.Date;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WebService extends BaseService {

    @Autowired
    private MaintenanceApplyMapper maintenanceApplyMapper;

    /**
     * 
     * @param reqBean
     * @return
     */
    public JSONObject resCheckFuzzyQuery(ReqBean reqBean) {
        String queryType = reqBean.getInfo("queryType");
        String param = reqBean.getInfo("param");
        if (StringUtils.isBlank(queryType)) {
            throw new BusinessException("缺少参数!");
        }
        String sql = "";
        if (BusConstant.MediaType.MEDIA_TYPE_BUS.equals(queryType)) { // 车辆
            sql += "SELECT CONCAT(b.VEHICLE_NO,' / ',b.PLATE_NO,' ( 线路:',b.LINE_NO,' )') showName FROM MEDIA_BUS b where 1=1 ";
            if (StringUtils.isNotBlank(param)) {
                sql += "and (b.VEHICLE_NO LIKE '%" + param + "%' or b.LINE_NO LIKE '%" + param
                        + "%' or b.PLATE_NO LIKE '%" + param + "%') order by b.BUS_ID DESC";
            }
        } else if (BusConstant.MediaType.MEDIA_TYPE_SHELTER.equals(queryType)) { // 站点
            System.out.println(param);
            sql += "SELECT CONCAT(s.STOP_NAME,' / ',s.SHELTER_NO,' ( 行向:',s.C_DIRECTION,' )') showName FROM MEDIA_SHELTER s where 1=1 ";
            if (StringUtils.isNotBlank(param)) {
                sql += " and (s.STOP_NAME LIKE '%" + param + "%' or s.SHELTER_NO LIKE '%" + param
                        + "%' or s.C_DIRECTION LIKE '%" + param + "%') order by s.STOP_ID DESC";
            }
        }
        return queryTable(new SqlBuilder(sql), reqBean.getPageSize(), reqBean.getPageIndex());
    }

    public JSONObject appScanCode(String maintenanceStr, String empId) {

        if (StringUtil.isEmpty(maintenanceStr) || StringUtil.isEmpty(empId) || "{}".equals(maintenanceStr)) {
            throw new BusinessException("非法请求!");
        }

        // 解密字符串
        // String decryptStr = null;
        // try {
        // DesUtils desUtils = new DesUtils();
        // decryptStr = desUtils.decrypt(maintenanceStr);
        // } catch (Exception e) {
        // e.printStackTrace();
        // return Response.responseError("解密异常");
        // }
        // JSONObject decryptJson = JSONObject.fromObject(decryptStr);
        // if (decryptJson == null || "{}".equals(decryptJson)) {
        // return Response.responseError("解密后的数据异常!");
        // }
        // long encryptDate =
        // Long.parseLong(decryptJson.getString("encryptDate"));

        JSONObject jsonObject = JSONObject.fromObject(maintenanceStr);
        String maintenanceId = jsonObject.getString("maintenanceId");
        MaintenanceApply maintenanceApply = maintenanceApplyMapper.selectByPrimaryKey(maintenanceId);

        return JSONObject.fromObject(maintenanceApply, JSONUtil.getJsonConfig());
    }

    /**
     * app报修提交
     * @param reqBean
     */
    public void appMtApply(ReqBean reqBean) {
        String maintenanceStr = reqBean.getInfo("maintenanceStr");
        String fileStr = reqBean.getInfo("fileStr");
        if(StringUtil.isEmpty(maintenanceStr) || BusConstant.EMPTY_JSON.equals(maintenanceStr)
                || StringUtil.isEmpty(fileStr) || BusConstant.EMPTY_JSON.equals(fileStr)) {
            throw new BusinessException("缺少参数!");
        }

        JSONObject maintenanceJson = JSONObject.fromObject(maintenanceStr);

        //插入维修信息

        //插入上传的文件


    }
}
